package com.example.mylearn.arthasTest;

import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Controller
@Slf4j
public class ArthasController {


//    @RequestMapping("111")
//    public Dream getInfo() {
//        Dream d = new Dream();
//        d.setRgb("\u9752\u6d77");
//        d.setName("1111");
//        return d;
//    }


    private final ObjectMapper customObjectMapper = new CustomObjectMapper();


    @ResponseBody
    @RequestMapping("111")
    public void getInfo(HttpServletResponse response) throws IOException {

        List<Map<String, Object>> list = Arrays.asList(
                new HashMap<String, Object>() {{
                    put("test", "\\u6D4B\\u8BD5");
                }}
        );

        // 设置响应头
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        String s = "{\"result\":" + convertToJsonString(list) + "}";
        // 获取响应的 PrintWriter
        response.getWriter().print(s);
    }



    @RequestMapping("callGetInfo")
    public void callGetInfo(HttpServletResponse response) throws IOException {

        String result1= HttpUtil.get("http://localhost:8080/111");



    }




    private String convertToJsonString(List<Map<String, Object>> data) {
        StringBuilder jsonBuilder = new StringBuilder("[");
        boolean isFirst = true;

        for (Map<String, Object> map : data) {
            if (!isFirst) {
                jsonBuilder.append(",");
            } else {
                isFirst = false;
            }

            jsonBuilder.append("{");
            boolean firstEntry = true;

            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if (!firstEntry) {
                    jsonBuilder.append(",");
                } else {
                    firstEntry = false;
                }

                // 手动处理字符串，避免转义
                jsonBuilder.append("\"").append(entry.getKey()).append("\":\"").append(entry.getValue()).append("\"");
            }

            jsonBuilder.append("}");
        }

        jsonBuilder.append("]");
        return jsonBuilder.toString();
    }



    private static ExecutorService threadPool = Executors.newCachedThreadPool();


    @RequestMapping("cpu-100")
    public static void cpu() {
        //无限循环输出打印
        new Thread(() -> {
            while (true) {
                log.info("cpu working");
            }
        }, "thread-1").start();
    }


    //两把锁
    private Object lock1 = new Object();
    private Object lock2 = new Object();

    @RequestMapping("dead-lock")
    public void deadLock() {
        //线程1先取得锁1，休眠后取锁2
        new Thread(() -> {
            synchronized (lock1) {
                try {
                    log.info("t1 successfully acquired the lock1......");
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


                synchronized (lock2) {
                    log.info("t1 successfully acquired the lock1......");
                }
            }
        }, "t1").start();

        //线程2先取得锁2，休眠后取锁1
        new Thread(() -> {
            synchronized (lock2) {
                try {
                    log.info("t2 successfully acquired the lock2......");
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


                synchronized (lock1) {
                    log.info("t2 successfully acquired the lock1......");
                }
            }
        }, "t2").start();
    }




}
